/*
 *  This class is part of WorkTimeCalculator Android App
 *  Author: Domenico Pio Novelli
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.dpndeveloper.wtc.dialogs;

import java.util.Calendar;
import java.util.TimeZone;

import android.content.Context;

import com.dpndeveloper.wtc.R;
import com.dpndeveloper.wtc.dataprovider.WtcInterval;
import com.dpndeveloper.wtc.dataprovider.WtcIntervalDataBaseHelper;
import com.dpndeveloper.wtc.listener.OnIntervalSetListener;

/**
 * Extension of Interval dialog to set the start of interval for current day
 * 
 * @author Domenico Pio Novelli
 * 
 */
public class StartAtDialog extends IntervalDialog {

	private WtcInterval	m_previousInterval;

	/**
	 * Constructor
	 * 
	 * @param context
	 *            The context where the View associated with this Dialog is
	 *            running
	 * @param previousInterval
	 *            the previous interval used to validate start time
	 * @param onIntervalSetListener
	 *            an OnIntervalSetListener
	 * @param databaseHelper
	 *            Instance of DatabaseHelper to manage WtcIntervals
	 */
	public StartAtDialog(Context context, WtcInterval previousInterval,
			OnIntervalSetListener onIntervalSetListener,
			WtcIntervalDataBaseHelper databaseHelper) {

		super(context, null, onIntervalSetListener, databaseHelper, true);
		setTitle(R.string.workStartAt);
		m_previousInterval = previousInterval;

	}

	public void setLastInterval(WtcInterval previousInterval) {
		m_previousInterval = previousInterval;
	}

	@Override
	protected boolean isValid() {

		int startHourOfDay = getStartPicker().getCurrentHour();
		int startMinute = getStartPicker().getCurrentMinute();
		int previousHourOfDay = 0;
		int previousMinute = 0;

		if (m_previousInterval != null
				&& m_previousInterval.getEndTime() != null) {
			previousHourOfDay = m_previousInterval.getEndTime().get(
					Calendar.HOUR_OF_DAY);
			previousMinute = m_previousInterval.getEndTime().get(
					Calendar.MINUTE);
		}

		Calendar time = Calendar.getInstance(TimeZone.getDefault());
		// If the set start time is in the future
		return !(time.get(Calendar.HOUR_OF_DAY) < startHourOfDay
				|| (time.get(Calendar.HOUR_OF_DAY) == startHourOfDay && time
						.get(Calendar.MINUTE) < startMinute) ||
		// If the set start time is before the previous end time
		(previousHourOfDay > startHourOfDay || (previousHourOfDay == startHourOfDay && previousMinute > startMinute)));
	}

}
